<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class NavCateController extends AdminBaseController
{
    const CNAME = '导航分类';

    protected $navCate_model;

    public function _initialize() {
        parent::_initialize();
        $this->navCate_model = D("Common/NavCate");

        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 导航分类列表
     */
    public function index()
    {
        $this->assign('list', $this->navCate_model->fetchAll());
        $this->display();
    }

    /**
     * 添加导航分类
     */
    public function add()
    {
        if (IS_POST) {
            if(I('post.is_main',0,'intval') === 1){
                $this->navCate_model->where("is_main=1")->save(array("is_main"=>0));
            }
            $data = $this->navCate_model->create();
            if ($data) {
                $id = $this->navCate_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->navCate_model->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 修改导航分类
     */
    public function edit() {
        if (IS_POST) {
            if(I('post.is_main',0,'intval') === 1){
                $this->navCate_model->where("is_main=1")->save(array("is_main"=>0));
            }
            $data = $this->navCate_model->create();
            if ($data) {
                if ($this->navCate_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->navCate_model->getError());
            }
        } else {
            $cate_id = I('get.cate_id',0,'intval');
            $detail = $this->navCate_model->find($cate_id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除导航分类
     */
    public function delete() {
        $cate_id = I('post.cate_id',0,'intval');
        if (is_numeric($cate_id)) {

            $detail = $this->navCate_model->find($cate_id);

            if (!$detail) {
                $this->error('请选择要删除的导航分类');
            }

            if ($detail['is_main'] == 1) {
                $this->error('删除主导航前请先设置其他导航为主导航');
            }

            $this->navCate_model->delete($cate_id);
            $nav_model = D("Common/Nav");
            $nav_model->where(array('cate_id' => $cate_id))->delete();
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的导航分类');
        }
    }
}